package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.partolStore.EquipmentBo;
import com.ruoyi.wms.domain.entity.partolStore.Equipment;
import com.ruoyi.wms.domain.vo.EquipmentVo;
import com.ruoyi.wms.domain.vo.NameAndIdList;
import com.ruoyi.wms.mapper.EquipmentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 设备信息档案Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-24
 */
@RequiredArgsConstructor
@Service
public class EquipmentService {

    private final EquipmentMapper equipmentMapper;

    /**
     * 查询设备信息档案
     */
    public EquipmentVo queryById(Long id) {
        return equipmentMapper.selectVoById(id);
    }

    /**
     * 查询设备信息档案列表
     */
    public TableDataInfo<EquipmentVo> queryPageList(EquipmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Equipment> lqw = buildQueryWrapper(bo);
        Page<EquipmentVo> result = equipmentMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备信息档案列表
     */
    public List<EquipmentVo> queryList(EquipmentBo bo) {
        LambdaQueryWrapper<Equipment> lqw = buildQueryWrapper(bo);
        return equipmentMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Equipment> buildQueryWrapper(EquipmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Equipment> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), Equipment::getEquipmentName, bo.getEquipmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), Equipment::getModel, bo.getModel());
        lqw.eq(bo.getStoreId() != null, Equipment::getStoreId, bo.getStoreId());
        lqw.between(params.get("beginWarrantyPeriod") != null && params.get("endWarrantyPeriod") != null,
            Equipment::getWarrantyPeriod, params.get("beginWarrantyPeriod"), params.get("endWarrantyPeriod"));
        lqw.eq(bo.getStatus() != null, Equipment::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getNotes()), Equipment::getNotes, bo.getNotes());
        return lqw;
    }

    /**
     * 新增设备信息档案
     */
    public void insertByBo(EquipmentBo bo) {
        Equipment add = MapstructUtils.convert(bo, Equipment.class);
        equipmentMapper.insert(add);
    }

    /**
     * 修改设备信息档案
     */
    public void updateByBo(EquipmentBo bo) {
        Equipment update = MapstructUtils.convert(bo, Equipment.class);
        equipmentMapper.updateById(update);
    }

    /**
     * 批量删除设备信息档案
     */
    public void deleteByIds(Collection<Long> ids) {
        equipmentMapper.deleteBatchIds(ids);
    }

    /**
     * 获取设备名称和id
     *
     * @return
     */
    public List<NameAndIdList> getEquipmentList() {
        return
            equipmentMapper.selectList().stream().map(item -> {
                NameAndIdList nameAndIdList = new NameAndIdList();
                nameAndIdList.setId(item.getId());
                nameAndIdList.setName(item.getEquipmentName());
                return nameAndIdList;
            }).toList();
    }
}
